package retrofitdemo.com.night.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;
import retrofitdemo.com.night.R;
import retrofitdemo.com.night.activity.MoreActivity;
import retrofitdemo.com.night.adapter.CommendAdapter;
import retrofitdemo.com.night.bean.CommendCallback;
import retrofitdemo.com.night.bean.FindListCallback;
import retrofitdemo.com.night.databinding.FragmentFirstBinding;
import retrofitdemo.com.night.utils.GsonUtil;
import retrofitdemo.com.night.utils.HTTPAPI;
import retrofitdemo.com.night.utils.LogUtils;

public class FirstFragment extends Fragment implements View.OnClickListener {

    FragmentFirstBinding binding;
    List<FindListCallback.ResultBean> list = new ArrayList<>();
    FindListCallback.ResultBean resultBean;
    int location = 0;
    boolean com = true;

    List<CommendCallback.ResultBean> listCommonend = new ArrayList<>();
    List<CommendCallback.ResultBean> listSecondCommend = new ArrayList<>();
    CommendAdapter commendAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_first, container, false);
        //页面数据初始化方法
        disposeData();
        binding.setFragment(this);
        return binding.getRoot();
    }

    private boolean mB_dataFirst = true;

    private void disposeData() {
        if (mB_dataFirst) {
            initData();
            mB_dataFirst = !mB_dataFirst;
        }
        refreshData();
    }

    /**
     * 初始化数据
     */
    public void initData() {
        commendAdapter = new CommendAdapter(getActivity(), listCommonend);
        binding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recycler.setItemAnimator(new DefaultItemAnimator());
        binding.recycler.setAdapter(commendAdapter);

        findList();
        binding.btnModuleMore.setOnClickListener(this);
        binding.btnModuleNext.setOnClickListener(this);
        binding.ivModuleCommint.setOnClickListener(this);
        binding.btnModuleCommint.setOnClickListener(this);
    }

    /**
     * 刷新数据
     */
    public void refreshData() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnModuleMore) {
            LogUtils.e("更多");
            if (resultBean != null) {
                startActivity(new Intent(getActivity(), MoreActivity.class).putExtra("module_bean", resultBean));
            }
        } else if (id == R.id.btnModuleNext) {
            LogUtils.e("下一条");
            if (list.size() > location + 1) {
                resultBean = list.get(++location);
                binding.setBean(resultBean);
                findCommonend(resultBean.getId());
            }
        } else if (id == R.id.ivModuleCommint) {
            if (com) {
                com = false;
                binding.llModuleCommint.setVisibility(View.VISIBLE);
            } else {
                com = true;
                binding.llModuleCommint.setVisibility(View.GONE);
            }
        } else if (id == R.id.btnModuleCommint) {
            commint();
        }
    }

    public void findList() {
        HTTPAPI.getInstance().findList(getActivity(), "1", new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("古诗词-->" + s);
                FindListCallback bean = GsonUtil.GsonToBean(s, FindListCallback.class);
                if ("200".equals(bean.getCode())) {
                    list = bean.getResult();
                    resultBean = list.get(0);
                    binding.setBean(resultBean);
                    findCommonend(resultBean.getId());
                }
            }
        });
    }

    public void findCommonend(String id) {
        HTTPAPI.getInstance().findListCommoned(getActivity(), id, "1", new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("五条评论-->" + s);
                CommendCallback callback = GsonUtil.GsonToBean(s, CommendCallback.class);
                if ("200".equals(callback.getCode())) {
                    listCommonend.clear();
                    listSecondCommend = callback.getResult();
                    listCommonend.addAll(listSecondCommend);
                    commendAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    public void commint() {
        String comment = binding.etModuleComment.getText().toString().trim();
        if (TextUtils.isEmpty(comment)) {
            Toast.makeText(getActivity(),"请输入评论",Toast.LENGTH_SHORT).show();
            return;
        }

        HTTPAPI.getInstance().commint(getActivity(), resultBean.getId(), comment,
                new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtils.e("评论-->" + s);
                        binding.etModuleComment.setText("");
                        findCommonend(resultBean.getId());
                    }
                });
    }
}
